home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / POSIX / ThinkCPosix / stat.c < prev    next >
Text File  |  1992-09-11  |  2KB  |  124 lines

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * Minimal 'stat' emulation: tells directories from files and
  5.  * gives length and mtime.
  6.  *
  7.  * Largely based on code written by Guido van Rossum, CWI, Amsterdam
  8.  * and placed by him in the public domain --
  9.  * retrieved by anonymous FTP from ftp.cwi.nl
  10.  */
  11.  
  12. #include "ThinkCPosix.h"
  13.  
  14. int sys_nerr = 0;
  15. char *sys_errlist[] = {""};
  16. char *myenviron[] = {NULL};
  17. char **environ = myenviron;
  18. extern int __uid, __gid;
  19. int Stat(char*, long, struct stat*);
  20.  
  21.  
  22. /* Bits in ioFlAttrib: */
  23. #define LOCKBIT    (1<<0)        /* File locked */
  24. #define DIRBIT    (1<<4)        /* It's a directory */
  25.  
  26. /*
  27.  * Mac-ky "stat" in which filename is given relative to a directory,
  28.  * specified by long DirID.
  29.  */
  30.  
  31. int
  32. Stat(name, DirID, buf)
  33.     char *name;
  34.     long DirID;
  35.     struct stat *buf;
  36. {
  37.     CInfoPBRec cipbr;
  38.     HFileInfo *fpb = (HFileInfo*)&cipbr;
  39.     DirInfo *dpb = (DirInfo*)&cipbr;
  40.     unsigned char pname[256];
  41.     short err;
  42.  
  43.     strcpy((char*)pname, name);
  44.     c2pstr(pname);
  45.  
  46.     dpb->ioDrDirID = DirID;
  47.     fpb->ioNamePtr = pname;
  48.     fpb->ioVRefNum = 0;
  49.     fpb->ioFDirIndex = 0;
  50.     fpb->ioFVersNum = 0;
  51.     err = PBGetCatInfo(&cipbr, FALSE);
  52.     if (err != noErr) {
  53.         errno = ENOENT;
  54.         return -1;
  55.     }
  56.     if (fpb->ioFlAttrib & LOCKBIT)
  57.         buf->st_mode= 0444;
  58.     else
  59.         buf->st_mode= 0666;
  60.     if (fpb->ioFlAttrib & DIRBIT) {
  61.         buf->st_mode |= 0111 | S_IFDIR;
  62.         buf->st_size= dpb->ioDrNmFls;
  63.         buf->st_rsize= 0;
  64.     }
  65.     else {
  66.         buf->st_mode |= S_IFREG;
  67.         if (fpb->ioFlFndrInfo.fdType == 'APPL')
  68.             buf->st_mode |= 0111;
  69.         buf->st_size= fpb->ioFlLgLen;
  70.         buf->st_rsize= fpb->ioFlRLgLen;
  71.     }
  72.     buf->st_mtime= fpb->ioFlMdDat;
  73.     buf->st_ctime= fpb->ioFlCrDat;
  74.     buf->st_ino= (unsigned short)fpb->ioDirID;
  75.     buf->st_uid= __uid;
  76.     buf->st_gid= __gid;
  77.     return 0;
  78. }
  79.  
  80. int
  81. stat(path, buf)
  82.     char *path;
  83.     struct stat *buf;
  84. {
  85.     return Stat(path, 0L, buf);
  86. }
  87.  
  88. int
  89. fstat(fd, buf)
  90.     int fd;
  91.     struct stat *buf;
  92. {
  93.     FCBPBRec fcb;
  94.     unsigned char pname[256];
  95.     long DirID;
  96.     short err;
  97.     
  98.     /*
  99.      * fdopen() gives FILE entry with name of file,
  100.      * as well as RefNum of containing directory
  101.      */
  102.     
  103.     FILE *fp = fdopen(fd, "");
  104.     
  105.     /* 
  106.      * PBGetFCBInfo() converts short RefNum to long DirID
  107.      */
  108.      
  109.     fcb.ioRefNum= fp->refnum;
  110.     fcb.ioVRefNum= 0;
  111.     fcb.ioFCBIndx= 0;
  112.     fcb.ioNamePtr= pname;
  113.     err= PBGetFCBInfo(&fcb, FALSE);
  114.     if (err != noErr) {
  115.         errno = ENOENT;
  116.         return -1;
  117.     }
  118.     DirID = fcb.ioFCBParID;
  119.     
  120.     p2cstr(pname);
  121.     return Stat((char*)pname, DirID, buf);
  122. }
  123.  
  124.